<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class personil extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_personil');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        $this->load->library('datetimemanipulation');
        $this->smarty->assign("dtm", $this->datetimemanipulation);
        // load javascript
        $this->smarty->load_javascript("resource/js/jquery/jquery-ui-1.9.2.custom.min.js");
        $this->smarty->load_style("jquery.ui/redmond/jquery-ui-1.8.13.custom.css");
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "master/personil/list.html");

        //pencarian
        $search = $this->tsession->userdata('master_personil_search');
        $this->smarty->assign("search", $search);
        $personil_name = empty($search['personil_nama']) ? '%' : '%' . $search['personil_nama'] . '%'; 
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/personil/index/");
        $config['total_rows'] = $this->m_personil->get_count_all_personil_search(array($personil_name));
        $config['uri_segment'] = 4;
        $config['per_page'] = 10;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($personil_name, ($start - 1), $config['per_page']);
        $this->smarty->assign("rs_id", $this->m_personil->get_all_personil_pagination_search($params));
        
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function proses_cari() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('master_personil_search');
        } else {
            $params = array(
                "personil_nama" => $this->input->post("personil_nama") 
            );
            $this->tsession->set_userdata("master_personil_search", $params);
        }
        // redirect
        redirect("master/personil");
    }

    public function add() {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/personil/add.html");
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // add process
    public function add_process() {
        // cek input  
        $this->tnotification->set_rules('personil_nama', 'Nama', 'trim|required|max_length[200]'); 

        // process 
        if ($this->tnotification->run() !== FALSE) { 
            // load
            if(!empty($_FILES['personil_picture']['tmp_name'])) { 
                $this->load->library('tupload');
                $time = microtime(true);
                $time = str_replace('.', '', $time);
                // upload config
                $config['upload_path'] = 'resource/userfile/personil/';
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['max_size'] = '1000';
                $config['max_width'] = '1024';
                $config['max_height'] = '800';
                $config['file_name'] = $time;
                $this->tupload->initialize($config);
                if(!$this->tupload->do_upload_image('personil_picture')){
                    // jika gagal (kembalikan pesan)
                    $this->tnotification->set_error_message($this->tupload->display_errors());
                    $this->tnotification->sent_notification("error", "Foto gagal diupload");
                    $foto = null;
                } else {
                    $data_upload = $this->tupload->data();
                    $foto = $data_upload['file_name']; 
                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                }
            }else{
                $foto = null;
            } 
            
            // parameter
            $params = array(
                $this->input->post('personil_nama'),
                $foto,
                $this->input->post('personil_notelpon'),
                $this->input->post('personil_tgllahir'),
                $this->com_user['user_id']
            );
            // insert
            if ($this->m_personil->insert($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            } 
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/personil/add");
    }

    public function edit($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/personil/edit.html");
        //data
        $result = $this->m_personil->get_personil_by_id($id);
        $this->smarty->assign("result", $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        // cek input 
        $this->tnotification->set_rules('personil_id', 'Kode', 'trim|required');
        $this->tnotification->set_rules('personil_nama', 'Nama', 'trim|required|max_length[200]'); 

        // process
        if ($this->tnotification->run() !== FALSE) {
            // load
            $this->load->library('tupload');
            $time = microtime(true);
            $time = str_replace('.', '', $time);
            $result = $this->m_personil->get_personil_by_id($this->input->post('personil_id'));
            $foto = $result['personil_picture'];
            
            if(!empty($_FILES['personil_picture']['tmp_name'])) { 
                // upload config
                $config['upload_path'] = 'resource/userfile/personil/';
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['max_size'] = '1000';
                $config['max_width'] = '1024';
                $config['max_height'] = '800';
                $config['file_name'] = $time;
                $this->tupload->initialize($config);
                if(!$this->tupload->do_upload_image('personil_picture')){
                    // jika gagal (kembalikan pesan)
                    $this->tnotification->set_error_message($this->tupload->display_errors());
                    $this->tnotification->sent_notification("error", "Foto gagal diupload");
                    $foto = null;
                } else {
                    unlink('resource/userfile/personil/'.$foto);
                    $data_upload = $this->tupload->data();
                    $foto = $data_upload['file_name']; 
                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                }
            }  
            // parameter
            $params = array(
                $this->input->post('personil_nama'),
                $foto,
                $this->input->post('personil_notelpon'),
                $this->input->post('personil_tgllahir'),
                $this->com_user['user_id'],
                $this->input->post('personil_id')
            ); 
            // insert
            if ($this->m_personil->edit($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/personil/edit/" . $this->input->post('personil_id'));
    }

    public function delete($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/personil/delete.html");
        //data
        $result = $this->m_personil->get_personil_by_id($id);
        $this->smarty->assign("result", $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        // cek input 
        $this->tnotification->set_rules('personil_id', 'Kode', 'trim|required|max_length[3]');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array($this->input->post('personil_id')); 
            $result = $this->m_personil->get_personil_by_id($params);  
            
            // insert
            if ($this->m_personil->delete($params)) { 
                unlink('resource/userfile/personil/'.$result['personil_picture']);
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/personil/");
    }

}
